# This is a sample Python script.
# -*- coding: UTF-8 -*-\
from datetime import date

from xToolkit.xstring import xstring

import toolkit
from pyxirr import xirr

from data_alchemy.account_voucher_alchemy import AccountVoucherAlchemy
from data_alchemy.investment_flow_alchemy import InvestmentFlowAlchemy
from initXirrPage import FLOW_TYPE_DING_TOU, FLOW_TYPE_TI_XIAN


def xirrTest():
    dates = [date(2022, 1, 1), date(2022, 12, 31)]
    amounts = [-1000, 1200]
    result = xirr(dates, amounts)
    print(result)

    dates = [date(2021, 1, 1), date(2021, 12, 31), date(2022, 12, 31)]
    amounts = [-1000, 0,1200]
    result = xirr(dates, amounts)
    print(result)

def print_hi(name):

    #value= 7009.999999999996
    value = 7009.999
    value_1 = toolkit.processValueSignificant(value)
    value_2 = xstring.dispose(value_1)
    #value_3 = value_2.humanized_amount(complex=True)
    value_3 = value_2.humanized_amount(complex=False)
    #value_3 = value_2.humanized_amount()
    print(value_3)
    #endValueStr = xstring.dispose(22).humanized_amount(complex=True)

    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

    dataStr= "11,000.00"
    newStr = dataStr.replace(",","")
    newfloat = float(newStr)
    print(newfloat)


def batchProcessInvestmentPutIntoData():
    '''
    函数功能：对investment data 进行批量处理
    :return:
    '''
    accountVoucherAlchemy = AccountVoucherAlchemy()
    investmentVoucherInfor =  accountVoucherAlchemy.SearchVoucherBySubject("property_increase_name","资产->投资->理财%")
    print(investmentVoucherInfor)

    for investmentVoucherItem in  investmentVoucherInfor:
        if investmentVoucherItem.property_increase_value == investmentVoucherItem.property_decrease_value:
            #资产增减相同，为实际资金投入；去除掉随出归做“历年积累”的资金，这部分资金后续单独计算。

            subStringList = investmentVoucherItem.property_increase_name.split("->")
            substringListLen = len(subStringList)
            investmentName = subStringList[substringListLen-1]
            investmentFlowAlchemy = InvestmentFlowAlchemy(investmentDate = investmentVoucherItem.account_date,
                                                      investmentValue = investmentVoucherItem.property_increase_value,
                                                      investmentPath = investmentVoucherItem.property_increase_name,
                                                      flowType = FLOW_TYPE_DING_TOU,
                                                      voucherId = investmentVoucherItem.id)
            investmentFlowAlchemy.AddInvestmentItem()


def batchProcessInvestmentFetchData():
    '''
    函数功能：批量处理投资金额取出的数据
    :return:
    '''
    accountVoucherAlchemy = AccountVoucherAlchemy()
    investmentVoucherInfor =  accountVoucherAlchemy.SearchVoucherBySubject("property_decrease_name","资产->投资->理财%")
    print(investmentVoucherInfor)

    for investmentVoucherItem in  investmentVoucherInfor:
        if investmentVoucherItem.property_increase_value == investmentVoucherItem.property_decrease_value:
            #资产增减相同，为实际资金投入；去除掉随出归做“历年积累”的资金，这部分资金后续单独计算。

            subStringList = investmentVoucherItem.property_decrease_name.split("->")
            substringListLen = len(subStringList)
            investmentName = subStringList[substringListLen - 1]
            investmentFlowAlchemy = InvestmentFlowAlchemy(investmentDate = investmentVoucherItem.account_date,
                                                      investmentValue = investmentVoucherItem.property_decrease_value*(-1),
                                                      investmentPath = investmentVoucherItem.property_decrease_name,
                                                      flowType = FLOW_TYPE_TI_XIAN,
                                                      voucherId = investmentVoucherItem.id)
            investmentFlowAlchemy.AddInvestmentItem()




def main():
    batchProcessInvestmentPutIntoData()   #对investment data 进行批量处理
    batchProcessInvestmentFetchData()  #批量处理投资金额取出的数据
    #xirrTest()


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    #print_hi('PyCharm')
    main()


# See PyCharm help at https://www.jetbrains.com/help/pycharm/
